/**
 * Copyright(C) 2008 - 2009 思敏工作室. All Rights Reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF 思敏工作室.
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */
package org.simin.smim.controller;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Server
 * 
 * @author ross
 * @version 0.1
 */
public class Server {
	/**
	 * Default network port
	 */
	public static final int PORT = 1219;

	private ServerSocket serverSocket;
	/**
	 * Thread pool
	 */
	private ExecutorService executorService;
	/**
	 * Pool size
	 */
	private static final int POOL_SIZE = 10;

	public Server() throws IOException {
		serverSocket = new ServerSocket(PORT);
		executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
				.availableProcessors()
				* POOL_SIZE);
	}

	public void service() {
		while (true) {
			Socket socket = null;
			try {
				socket = serverSocket.accept();
				executorService.execute(new Handler(socket));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
